package com.dgut.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dgut.service.CityRiskrankService;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
public class ImportantAreaController {

    private final CityRiskrankService cityRiskrankService;

    public ImportantAreaController(CityRiskrankService cityRiskrankService) {
        this.cityRiskrankService = cityRiskrankService;
    }


    //获取中高风险地区
    @RequestMapping("/importantArea")
    public String getImportantArea() {
        return cityRiskrankService.getImportantAreaFromRedis();
    }

    //设置防水墙参数
    private static final String APP_ID = "2047656516";
    private static final String APP_SECRET = "044rCKFCpe2VqP7Pfv2nz4A**";
    private static final String VERIFY_URI = "https://ssl.captcha.qq.com/ticket/verify?aid=%s&AppSecretKey=%s&Ticket=%s&Randstr=%s";

    //登录防水墙验证
    @RequestMapping(value = "login/verify", method = RequestMethod.GET)
    public Object verifyTicket(@RequestParam String ticket, @RequestParam String randstr) {
        Map<String, Object> data = new HashMap<>();
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpGet;
        CloseableHttpResponse response = null;
        try {
            httpGet = new HttpGet(String.format(VERIFY_URI, APP_ID, APP_SECRET, URLEncoder.encode(ticket, "UTF-8"),
                    URLEncoder.encode(randstr, "UTF-8")));
            response = httpclient.execute(httpGet);

            HttpEntity entity = response.getEntity();
            if (entity != null) {
                String res = EntityUtils.toString(entity);
                //System.out.println(res);
                JSONObject result = JSON.parseObject(res);
                //返回码
                int code = result.getInteger("response");
                //恶意等级
                int evilLevel = result.getInteger("evil_level");
                data.put("code", code);
                data.put("evilLevel", evilLevel);
                //验证成功
                if (code == 1) {
                    return data;
                }
            }

        } catch (java.io.IOException e) {
            try {
                response.close();

            } catch (Exception exception) {
            }
        }
        return -1;
    }


    //测试提交数据
//    @PostMapping("/handInfo")
//    public void handInfo(@RequestBody JSONObject jsonObject){
//        System.out.println(jsonObject);
//        System.out.println(jsonObject.get("temp"));
//    }
//
//    //测试传递数据到前端
//    @PostMapping("/getLoginInfo")
//    public LoginInfo getLoginInfo(){
//        LoginInfo loginInfo = new LoginInfo();
//        loginInfo.setId("201941404220");
//        loginInfo.setName("林啊顿");
//        loginInfo.setSchool("网络空间安全学院");
//        loginInfo.setType("学生用户");
//        return loginInfo;
//    }
}
